﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DTO;
using BUS;

namespace WEBSITE_BANDEAL.Admin
{
    public partial class LoaiBV_Edit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            lit_title.Text = "Update loại sản phẩm";
            if (!IsPostBack)
            {
                LoadData();
            }

        }

        private void LoadData()
        {
            MaLoaiCha.DataSource = LOAI_BAI_VIETBUS.GetAll();
            MaLoaiCha.DataTextField = "Tenloai";
            MaLoaiCha.DataValueField = "Maloai";
            MaLoaiCha.DataBind();
            MaLoaiCha.Items.Insert(0, new ListItem("----NO PARENT---", "0"));
            try
            {
                int id = int.Parse(Request.QueryString["ID"]);
                LOAI_BAI_VIET lsp = LOAI_BAI_VIETBUS.GetByID(id);
                if (lsp != null)
                {
                    Ten.Value = lsp.Tenloai.ToString();
                    MaLoaiCha.SelectedValue = lsp.Maloaicha.ToString();
                    Ma.Value = lsp.Maloai.ToString();
                }
            }
            catch
            {
                Response.Redirect("LoaiSP_List.aspx");
            }
        }

        protected void btn_Update_Click(object sender, EventArgs e)
        {
            try
            {
                LOAI_BAI_VIET lsp = new LOAI_BAI_VIET();
                lsp.Maloaicha = int.Parse(MaLoaiCha.SelectedValue.ToString());
                lsp.Maloai = int.Parse(Ma.Value.ToString());
                lsp.Tenloai = Ten.Value;
                lsp.Trangthai = 1;
                LOAI_BAI_VIETBUS.update(lsp);
                Response.Write("<script type='text/javascript'>alert('Update thành công'); window.opener.location.reload(true); window.close()</script>");
            }
            catch
            {
                Response.Redirect("LoaiBV_List.aspx");
            }
        }
    }
}